package com.hauwei.huaweishop.service.impl;

import com.hauwei.huaweishop.dao.AdminsDao;
import com.hauwei.huaweishop.entity.Admins;
import com.hauwei.huaweishop.service.AdminsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

@Service
@Transactional
public class AdminsServiceImpl implements AdminsService {
    @Autowired
    private AdminsDao adminsDao;

    /**
     * 添加管理员方法
     * @param admins
     */
    @Override
    public void addAdmins(Admins admins) {
        adminsDao.addAdmins(admins);
    }

    /**
     * 根据名称查询管理员
     * @param ausername
     * @return
     */
    @Override
    public Admins queryAdminByAUsername(String ausername) {
        return adminsDao.queryAdminByAUsername(ausername);
    }

    /**
     * 管理员登录方法
     * @param admins
     * @return
     */
    @Override
    public Admins adminstoLogin(Admins admins) {
        // 查询数据库里又没有用户
        Admins adminsDB = adminsDao.queryAdminByAUsername(admins.getAusername());
        if (!ObjectUtils.isEmpty(adminsDB)){
            // 判断用户密码是否正确
            if (admins.getApassword().equals(adminsDB.getApassword())){
                return adminsDB;
            }else {
                throw new RuntimeException("密码不存在");
            }
        }else {
            throw new RuntimeException("用户不存在");
        }
    }

}
